home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GSMATRIX.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  10KB  |  311 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsmatrix.c */
  20. /* Matrix operators for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxarith.h"
  26. #include "gxmatrix.h"
  27.  
  28. /* The identity matrix */
  29. /* This should be private (static), but the interpreter */
  30. /* has to be able to fill in the "unused" words. */
  31. /*static*/ gs_matrix gs_identity_matrix =
  32.     { identity_matrix_body };
  33.  
  34. /* ------ Matrix creation ------ */
  35.  
  36. /* Create an identity matrix */
  37. void
  38. gs_make_identity(gs_matrix *pmat)
  39. {    *pmat = gs_identity_matrix;
  40. }
  41.  
  42. /* Create a translation matrix */
  43. int
  44. gs_make_translation(floatp dx, floatp dy, register gs_matrix *pmat)
  45. {    *pmat = gs_identity_matrix;
  46.     pmat->tx = dx;
  47.     pmat->ty = dy;
  48.     return 0;
  49. }
  50.  
  51. /* Create a scaling matrix */
  52. int
  53. gs_make_scaling(floatp sx, floatp sy, register gs_matrix *pmat)
  54. {    *pmat = gs_identity_matrix;
  55.     pmat->xx = sx;
  56.     pmat->yy = sy;
  57.     return 0;
  58. }
  59.  
  60. /* Create a rotation matrix. */
  61. /* The angle is in degrees. */
  62. int
  63. gs_make_rotation(floatp ang, register gs_matrix *pmat)
  64. {    float theta = ang * (M_PI / 180.0);
  65.     *pmat = gs_identity_matrix;
  66.     pmat->xx = pmat->yy = cos(theta);
  67.     pmat->yx = -(pmat->xy = sin(theta));
  68.     return 0;
  69. }
  70.  
  71. /* ------ Matrix arithmetic ------ */
  72.  
  73. /* Multiply two matrices.  We should check for floating exceptions, */
  74. /* but for the moment it's just too awkward. */
  75. /* Since this is used heavily, we check for shortcuts. */
  76. int
  77. gs_matrix_multiply(const gs_matrix *pm1, const gs_matrix *pm2, register gs_matrix *pmr)
  78. {    float xx1 = pm1->xx, yy1 = pm1->yy;
  79.     float tx1 = pm1->tx, ty1 = pm1->ty;
  80.     float xx2 = pm2->xx, yy2 = pm2->yy;
  81.     float xy2 = pm2->xy, yx2 = pm2->yx;
  82.     if ( !is_skewed(pm1) )
  83.        {    pmr->tx = tx1 * xx2 + pm2->tx;
  84.         pmr->ty = ty1 * yy2 + pm2->ty;
  85.         if ( is_fzero(xy2) )
  86.             pmr->xy = 0;
  87.         else
  88.             pmr->xy = xx1 * xy2,
  89.             pmr->ty += tx1 * xy2;
  90.         pmr->xx = xx1 * xx2;
  91.         if ( is_fzero(yx2) )
  92.             pmr->yx = 0;
  93.         else
  94.             pmr->yx = yy1 * yx2,
  95.             pmr->tx += ty1 * yx2;
  96.         pmr->yy = yy1 * yy2;
  97.        }
  98.     else
  99.        {    pmr->xx = xx1 * xx2 + pm1->xy * yx2;
  100.         pmr->xy = xx1 * xy2 + pm1->xy * yy2;
  101.         pmr->yy = pm1->yx * xy2 + yy1 * yy2;
  102.         pmr->yx = pm1->yx * xx2 + yy1 * yx2;
  103.         pmr->tx = tx1 * xx2 + ty1 * yx2 + pm2->tx;
  104.         pmr->ty = tx1 * xy2 + ty1 * yy2 + pm2->ty;
  105.        }
  106.     return 0;
  107. }
  108.  
  109. /* Invert a matrix.  Return gs_error_undefinedresult if not invertible. */
  110. int
  111. gs_matrix_invert(register const gs_matrix *pm, register gs_matrix *pmr)
  112. {    /* We have to be careful about fetch/store order, */
  113.     /* because pm might be the same as pmr. */
  114.     if ( !is_skewed(pm) )
  115.        {    if ( is_fzero(pm->xx) || is_fzero(pm->yy) )
  116.             return_error(gs_error_undefinedresult);
  117.         pmr->tx = - (pmr->xx = 1.0 / pm->xx) * pm->tx;
  118.         pmr->xy = 0.0;
  119.         pmr->yx = 0.0;
  120.         pmr->ty = - (pmr->yy = 1.0 / pm->yy) * pm->ty;
  121.        }
  122.     else
  123.        {    double det = pm->xx * pm->yy - pm->xy * pm->yx;
  124.         float mxx = pm->xx, mtx = pm->tx;
  125.         if ( det == 0 ) return_error(gs_error_undefinedresult);
  126.         pmr->xx = pm->yy / det;
  127.         pmr->xy = - pm->xy / det;
  128.         pmr->yx = - pm->yx / det;
  129.         pmr->yy = mxx / det;    /* xx is already changed */
  130.         pmr->tx = - (mtx * pmr->xx + pm->ty * pmr->yx);
  131.         pmr->ty = - (mtx * pmr->xy + pm->ty * pmr->yy);    /* tx is already changed */
  132.        }
  133.     return 0;
  134. }
  135.  
  136. /* Rotate a matrix, possibly in place.  The angle is in degrees. */
  137. int
  138. gs_matrix_rotate(register const gs_matrix *pm, floatp ang, register gs_matrix *pmr)
  139. {    float mxx, mxy;
  140.     int quads;
  141.     float tsin, tcos;
  142.     /* We do some special checking for multiples of 90, */
  143.     /* so we don't get any rounding errors. */
  144.     if (    ang >= -360 && ang <= 360 &&
  145.         ang == (quads = (int)ang / 90) * 90
  146.        )
  147.        {    int isin = 0, icos = 1, t;
  148.         quads &= 3;
  149.         while ( quads-- ) t = isin, isin = icos, icos = -t;
  150.         tsin = isin, tcos = icos;
  151.        }
  152.     else
  153.        {    float theta = ang * (M_PI / 180.0);
  154.         tsin = sin(theta);
  155.         tcos = cos(theta);
  156.        }
  157.     mxx = pm->xx, mxy = pm->xy;
  158.     pmr->xx = tcos * mxx + tsin * pm->yx;
  159.     pmr->xy = tcos * mxy + tsin * pm->yy;
  160.     pmr->yx = tcos * pm->yx - tsin * mxx;
  161.     pmr->yy = tcos * pm->yy - tsin * mxy;
  162.     pmr->tx = pm->tx;
  163.     pmr->ty = pm->ty;
  164.     return 0;
  165. }
  166.  
  167. /* ------ Coordinate transformations (floating point) ------ */
  168.  
  169. /* Note that all the transformation routines take separate */
  170. /* x and y arguments, but return their result in a point. */
  171.  
  172. /* Transform a point. */
  173. int
  174. gs_point_transform(floatp x, floatp y, register const gs_matrix *pmat,
  175.   register gs_point *ppt)
  176. {    ppt->x = x * pmat->xx + pmat->tx;
  177.     ppt->y = y * pmat->yy + pmat->ty;
  178.     if ( !is_fzero(pmat->yx) )
  179.         ppt->x += y * pmat->yx;
  180.     if ( !is_fzero(pmat->xy) )
  181.         ppt->y += x * pmat->xy;
  182.     return 0;
  183. }
  184.  
  185. /* Inverse-transform a point. */
  186. /* Return gs_error_undefinedresult if the matrix is not invertible. */
  187. int
  188. gs_point_transform_inverse(floatp x, floatp y, register const gs_matrix *pmat,
  189.   register gs_point *ppt)
  190. {    if ( !is_skewed(pmat) )
  191.        {    if ( is_fzero(pmat->xx) || is_fzero(pmat->yy) )
  192.             return_error(gs_error_undefinedresult);
  193.         ppt->x = (x - pmat->tx) / pmat->xx;
  194.         ppt->y = (y - pmat->ty) / pmat->yy;
  195.         return 0;
  196.        }
  197.     else
  198.        {    /* There are faster ways to do this, */
  199.         /* but we won't implement one unless we have to. */
  200.         gs_matrix imat;
  201.         int code = gs_matrix_invert(pmat, &imat);
  202.         if ( code < 0 ) return code;
  203.         return gs_point_transform(x, y, &imat, ppt);
  204.        }
  205. }
  206.  
  207. /* Transform a distance. */
  208. int
  209. gs_distance_transform(floatp dx, floatp dy, register const gs_matrix *pmat,
  210.   register gs_point *pdpt)
  211. {    pdpt->x = dx * pmat->xx;
  212.     pdpt->y = dy * pmat->yy;
  213.     if ( !is_fzero(pmat->yx) )
  214.         pdpt->x += dy * pmat->yx;
  215.     if ( !is_fzero(pmat->xy) )
  216.         pdpt->y += dx * pmat->xy;
  217.     return 0;
  218. }
  219.  
  220. /* Inverse-transform a distance. */
  221. /* Return gs_error_undefinedresult if the matrix is not invertible. */
  222. int
  223. gs_distance_transform_inverse(floatp dx, floatp dy,
  224.   register const gs_matrix *pmat, register gs_point *pdpt)
  225. {    if ( !is_skewed(pmat) )
  226.        {    if ( is_fzero(pmat->xx) || is_fzero(pmat->yy) )
  227.             return_error(gs_error_undefinedresult);
  228.         pdpt->x = dx / pmat->xx;
  229.         pdpt->y = dy / pmat->yy;
  230.        }
  231.     else
  232.        {    double det = pmat->xx * pmat->yy - pmat->xy * pmat->yx;
  233.         if ( det == 0 ) return_error(gs_error_undefinedresult);
  234.         pdpt->x = (dx * pmat->yy - dy * pmat->yx) / det;
  235.         pdpt->y = (dy * pmat->xx - dx * pmat->xy) / det;
  236.        }
  237.     return 0;
  238. }
  239.  
  240. /* Transform or inverse-transform a bounding box. */
  241. /* Return gs_error_undefinedresult if the matrix is not invertible. */
  242. private int
  243. bbox_transform_either(gs_rect *pbox_in, const gs_matrix *pmat,
  244.   gs_rect *pbox_out,
  245.   int (*point_xform)(P4(floatp, floatp, const gs_matrix *, gs_point *)),
  246.   int (*distance_xform)(P4(floatp, floatp, const gs_matrix *, gs_point *)))
  247. {    int code;
  248.     gs_point p, w, h;
  249.     double xmin, ymin, xmax, ymax;    /* gs_point uses double */
  250.     /* We must recompute the max and min after transforming, */
  251.     /* since a rotation may be involved. */
  252.     if (    (code = (*point_xform)(pbox_in->p.x, pbox_in->p.y, pmat, &p)) < 0 ||
  253.         (code = (*distance_xform)(pbox_in->q.x - pbox_in->p.x, 0.0, pmat, &w)) < 0 ||
  254.         (code = (*distance_xform)(0.0, pbox_in->q.y - pbox_in->p.y, pmat, &h)) < 0
  255.        )
  256.         return code;
  257.     xmin = xmax = p.x;
  258.     if ( w.x < 0 )    xmin += w.x;
  259.     else        xmax += w.x;
  260.     if ( h.x < 0 )    xmin += h.x;
  261.     else        xmax += h.x;
  262.     ymin = ymax = p.y;
  263.     if ( w.y < 0 )    ymin += w.y;
  264.     else        ymax += w.y;
  265.     if ( h.y < 0 )    ymin += h.y;
  266.     else        ymax += h.y;
  267.     pbox_out->p.x = xmin, pbox_out->p.y = ymin;
  268.     pbox_out->q.x = xmax, pbox_out->q.y = ymax;
  269.     return 0;
  270. }
  271. int
  272. gs_bbox_transform(gs_rect *pbox_in, const gs_matrix *pmat,
  273.   gs_rect *pbox_out)
  274. {    return bbox_transform_either(pbox_in, pmat, pbox_out,
  275.         gs_point_transform, gs_distance_transform);
  276. }
  277. int
  278. gs_bbox_transform_inverse(gs_rect *pbox_in, const gs_matrix *pmat,
  279.   gs_rect *pbox_out)
  280. {    return bbox_transform_either(pbox_in, pmat, pbox_out,
  281.         gs_point_transform_inverse, gs_distance_transform_inverse);
  282. }
  283.  
  284. /* ------ Coordinate transformations (to fixed point) ------ */
  285.  
  286. /* Transform a point with a fixed-point result. */
  287. int
  288. gs_point_transform2fixed(register const gs_matrix_fixed *pmat,
  289.   floatp x, floatp y, gs_fixed_point *ppt)
  290. {    ppt->x = float2fixed(x * pmat->xx) + pmat->tx_fixed;
  291.     ppt->y = float2fixed(y * pmat->yy) + pmat->ty_fixed;
  292.     if ( !is_fzero(pmat->yx) )
  293.         ppt->x += float2fixed(y * pmat->yx);
  294.     if ( !is_fzero(pmat->xy) )
  295.         ppt->y += float2fixed(x * pmat->xy);
  296.     return 0;
  297. }
  298.  
  299. /* Transform a distance with a fixed-point result. */
  300. int
  301. gs_distance_transform2fixed(register const gs_matrix_fixed *pmat,
  302.   floatp dx, floatp dy, gs_fixed_point *ppt)
  303. {    ppt->x = float2fixed(dx * pmat->xx);
  304.     ppt->y = float2fixed(dy * pmat->yy);
  305.     if ( !is_fzero(pmat->yx) )
  306.         ppt->x += float2fixed(dy * pmat->yx);
  307.     if ( !is_fzero(pmat->xy) )
  308.         ppt->y += float2fixed(dx * pmat->xy);
  309.     return 0;
  310. }
  311.